package com.fangfang.home;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import androidx.fragment.app.Fragment;
import com.common.base.fragment.BaseFragment;
import com.common.bean.InfoResult;
import com.common.ui.CustomViewPager;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.magicindicator.MagicIndicator;
import com.common.ui.magicindicator.ViewPagerHelper;
import com.common.ui.magicindicator.buildins.commonnavigator.CommonNavigator;
import com.common.ui.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import com.common.ui.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import com.common.ui.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import com.common.ui.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import com.fangfang.R;
import com.fangfang.home.adapter.HomeFragmentStatePagerAdapter;
import com.fangfang.home.views.ScaleTransitionPagerTitleView;
import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment {
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewpager)
    CustomViewPager viewpager;
    private HomeFragmentStatePagerAdapter homeFragmentStatePagerAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    public void init() {

        initViews();
        initMagicIndicator();
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return getTitles() == null ? 0 : getTitles().size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(getTitles().get(index));
                simplePagerTitleView.setTextSize(20);
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(Color.BLACK);//选中的标题字体颜色
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewpager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                /*BezierPagerIndicator indicator = new BezierPagerIndicator(context);
                indicator.setColors(
                        Color.parseColor("#ff4a42"),
                        Color.parseColor("#fcde64"),
                        Color.parseColor("#73e8f4"));
                return indicator;*/
                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewpager);
    }

    private void initViews() {
        homeFragmentStatePagerAdapter = new HomeFragmentStatePagerAdapter(getChildFragmentManager(),
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        homeFragmentStatePagerAdapter.setFragments(getItems());
        homeFragmentStatePagerAdapter.setTitles(getTitles());
        viewpager.setAdapter(homeFragmentStatePagerAdapter);
        viewpager.setCurrentItem(0);
        viewpager.setOffscreenPageLimit(3);
    }

    private List<String> getTitles() {
        ArrayList<String> titles = new ArrayList<>();
        titles.add("发现");
        titles.add("推荐");
        titles.add("日报");
        return titles;
    }

    private List<Fragment> getItems() {
        ArrayList<Fragment> items = new ArrayList<>();
        items.add(FindFragment.newInstance());
        items.add(RecommendFragment.newInstance());
        items.add(DailyPaperFragment.newInstance());
        return items;
    }
}
